/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.coupon.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.coupon.entity.FxWeightInfo;
import com.ykht.proj.coupon.mapper.FxWeightInfoMapper;
import com.ykht.proj.coupon.service.FxWeightInfoService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 权重详情表
 *
 * @author proj code generator
 * @date 2021-04-25 18:47:54
 */
@Service
public class FxWeightInfoServiceImpl extends ServiceImpl<FxWeightInfoMapper, FxWeightInfo> implements FxWeightInfoService {

//	@Override
//	public R saveAndCheck(FxWeightInfo fxWeightInfo) {
//		R r = this.updateCheck(fxWeightInfo);
//		this.save(fxWeightInfo);
//		return r;
//	}
//
//	@Override
//	public R updateAndCheck(FxWeightInfo fxWeightInfo) {
//		R r = this.updateCheck(fxWeightInfo);
//		this.updateById(fxWeightInfo);
//		return r;
//	}

//	@Override
//	public R saveCheck(FxWeightInfo info) {
//		List<FxWeightInfo> up = new ArrayList<>();
//		List<FxWeightInfo> down = new ArrayList<>();
//		List<FxWeightInfo> list = this.list(Wrappers.lambdaQuery(FxWeightInfo.class).eq(FxWeightInfo::getWeightType,info.getWeightType()).orderByDesc(FxWeightInfo::getLevel));
//
//		list.stream().forEach(o->{
//			if (o.getLevel()==info.getLevel())throw new RuntimeException("当前权重级别已存在");
//			if (o.getLevelValue()==info.getLevelValue())throw new RuntimeException("当前权重值已存在");
//
//			if (o.getLevel()>info.getLevel()){
//				up.add(o);
//			}else {
//				down.add(o);
//			}
//		});
//		this.opt(up,down,info);
//		return R.ok();
//	}

//	@Override
//	public R updateCheck(FxWeightInfo info) {
//		List<FxWeightInfo> up = new ArrayList<>();
//		List<FxWeightInfo> down = new ArrayList<>();
//		List<FxWeightInfo> list = this.list(Wrappers.lambdaQuery(FxWeightInfo.class).eq(FxWeightInfo::getWeightType,info.getWeightType()).orderByDesc(FxWeightInfo::getLevel));
//
//		list.stream().filter(o->info.getId()!=null?o.getId()!=info.getId():true).forEach(o->{
//			if (o.getLevel()==info.getLevel())throw new RuntimeException("当前权重级别已存在");
//			if (o.getLevelValue()==info.getLevelValue())throw new RuntimeException("当前权重值已存在");
//
//			if (o.getLevel()>info.getLevel()){
//				up.add(o);
//			}else {
//				down.add(o);
//			}
//		});
//		this.opt(up,down,info);
//		return R.ok();
//	}
//	private void opt(List<FxWeightInfo> up, List<FxWeightInfo> down,FxWeightInfo info) {
//		Optional<FxWeightInfo> optUp = up.stream().sorted(Comparator.comparing(FxWeightInfo::getLevel).reversed()).findFirst();
//		Optional<FxWeightInfo> optDown = down.stream().sorted(Comparator.comparing(FxWeightInfo::getLevel)).findFirst();
//		optUp.ifPresent(oo->{
//			if (info.getLevelValue().compareTo(oo.getLevelValue())<=0) throw new RuntimeException(String.format("当前权重值请小于%s",oo.getLevelValue()));
//		});
//		optDown.ifPresent(oo->{
//			if (info.getLevelValue().compareTo(oo.getLevelValue())>=0) throw new RuntimeException(String.format("当前权重值请大于%s",oo.getLevelValue()));
//		});
//
//	}

//	public static void main(String[] args) {
//
//		int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//
//		int max = Arrays.stream(numbers).reduce(42, (a, b) -> a > b ? a : b);  // 10
//		int max1 = Arrays.stream(numbers).reduce(0, Integer::max);            // 10
//
//		int min = Arrays.stream(numbers).reduce(0, (a, b) -> a < b ? a : b);  // 0
//		int min1 = Arrays.stream(numbers).reduce(0, Integer::min);
//		System.out.println(max);
//		System.out.println(max1);
//		System.out.println(min);
//		System.out.println(min1);
//	}
}
